//获取应用实例
var api = require("../../api.js");
// var area_picker = require("../../area-picker/area-picker.js");
var app = getApp();
Page({
  data: {
    name: "",
    mobile: "",
    detail: "",
    district: null,
    cityData: [],
    provinces: [],
    province: "选择省",
    citys: [],
    city: "选择市",
    countys: [],
    county: "选择区/县",
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    // code值
    province_id: "",
    city_id: "",
    district_id: "",
    // 省市区选择
    // region: ['请选择', '请选择', '请选择'],
  },

  bindChange: function (e) {
    var val = e.detail.value;
    var t = this.data.values;
    var cityData = this.data.cityData;
    this.setData({
      province_id: cityData[val[0]].code,
      city_id: cityData[val[0]].sub[val[1]].code,
      district_id: cityData[val[0]].sub[val[1]].sub[val[2]].code,
    });
    if (val[0] != t[0]) {
      const citys = [];
      const countys = [];
      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name);
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name);
      }
      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0],
        province_id: cityData[val[0]].code,
      });
      return;
    }
    if (val[1] != t[1]) {
      const countys = [];
      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name);
      }
      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0],
        city_id: cityData[val[0]].sub[val[1]].code,
      });
      return;
    }
    if (val[2] != t[2]) {
      this.setData({
        county: this.data.countys[val[2]],
        values: val,
        district_id: cityData[val[0]].sub[val[1]].sub[val[2]].code,
      });
      return;
    }
  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    });
    wx.hideKeyboard();
  },
  //end
  onLoad: function (options) {
    wx.showLoading({
      title: "正在加载",
    });
    setTimeout(function () {
      wx.hideLoading()
    }, 1000)
    var page = this;
    if (options.id) {
      
      app.request({
        url: api.user.address_detail,
        data: {
          id: options.id,
          token: app.globalData.token,
          // uid: app.globalData.Wxuser.user_id,
        },
        success: function (res) {
          wx.hideLoading();
          if (res.code == 1) {
            page.setData({
              name: res.data.name,
              detail: res.data.detail,
              province_id: res.data.province_id,
              city_id: res.data.city_id,
              district_id: res.data.district_id,
              province: res.data.province,
              city: res.data.city,
              county: res.data.county,
              mobile: res.data.mobile,
              address_id: res.data.address_id,
              is_default: res.data.is_default,
            });
          }
        }
      });
    }
    this.initData();
    
  },

  initData:function(){
    var that = this;

    var cityData = wx.getStorageSync("cityData");
    const provinces = wx.getStorageSync('provinces') ? wx.getStorageSync('provinces'):[];
    const citys = wx.getStorageSync('citys') ? wx.getStorageSync('citys'):[];
    const countys = wx.getStorageSync('countys') ? wx.getStorageSync('countys'):[];
    if(provinces.length<1){
      for (let i = 0; i < cityData.length; i++) {
        provinces.push(cityData[i].name);
      }
      wx.setStorageSync("provinces", provinces);
    }

    if (citys.length < 1) {
      for (let i = 0; i < cityData[0].sub.length; i++) {
        citys.push(cityData[0].sub[i].name);
      }
      wx.setStorageSync("citys", citys);
    }

    if (countys.length < 1) {
      for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
        countys.push(cityData[0].sub[0].sub[i].name);
      }
      wx.setStorageSync("countys", countys);
    }
    that.setData({
      provinces: provinces,
      citys: citys,
      countys: countys,
      cityData: cityData,
      // province: cityData[0].name,
      // city: cityData[0].sub[0].name,
      // county: cityData[0].sub[0].sub[0].name
    });
  },



  saveAddress: function () {
    var page = this;
    var myreg = /^([0-9]{6,12})$/;
    if (!myreg.test(page.data.mobile)) {
      wx.showToast({
        title: "联系电话格式不正确",
        image: "/images/icon-warning.png"
      });
      return false;
    }
    if (page.data.name == '') {
      wx.showToast({
        title: "请填写收货人",
        image: "/images/icon-warning.png"
      });
      return false;
    }
    if (page.data.detail == '') {
      wx.showToast({
        title: "请填写详细地址",
        image: "/images/icon-warning.png"
      });
      return false;
    }
    wx.showLoading({
      title: "正在保存",
      mask: true
    });
    var district = page.data.district;
    if (!district) {
      district = {
        province: {
          id: page.data.province_id
        },
        city: {
          id: page.data.city_id
        },
        district: {
          id: page.data.district_id
        }
      };
    }
    if (!district.province.id || !district.city.id || !district.district.id) {
      wx.showToast({
        title: "请选择所在地区",
        image: "/images/icon-warning.png"
      });
      return false;
    }
    app.request({
      url: api.user.address_save,
      method: "post",
      data: {
        address_id: page.data.address_id || "",
        is_default: page.data.is_default || "",
        name: page.data.name,
        mobile: page.data.mobile,
        province_id: district.province.id,
        city_id: district.city.id,
        district_id: district.district.id,
        detail: page.data.detail,
        token: app.globalData.token,
        // uid: app.globalData.Wxuser.user_id
      },
      success: function (res) {
        wx.hideLoading();
        if (res.code == 1) {
          wx.showModal({
            title: "提示",
            content: res.msg,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack();
              }
            }
          });
        }
        if (res.code == 0) {
          wx.showToast({
            title: res.msg,
            image: "/images/icon-warning.png"
          });
        }
      }
    });
  },

  inputBlur: function (e) {
    this.setData({
      condition: false
    });
    var name = e.currentTarget.dataset.name;
    var value = e.detail.value;
    //var data = '{"form":{"' + name + '":"' + value + '"}}';
    var data = '{"' + name + '":"' + value + '"}';
    this.setData(JSON.parse(data));
  },

  onReady: function () {},
  onShow: function () {
  }
});
